package com.henu.ocr.consumer;

import com.henu.ocr.CommonConstant;
import com.henu.ocr.entity.Company;
import com.henu.ocr.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Component
@SuppressWarnings("all")
public class DataDistributionMQConsumer {
    @Resource
    private CompanyService companyService;

//    @RabbitHandler
//    @RabbitListener(queues = (CommonConstant.QUEUE_NAME))
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(CommonConstant.QUEUE_NAME), key = CommonConstant.ROUTING_KEY, exchange = @Exchange(CommonConstant.EXCHANGE_NAME)))
    public void process(@Payload Map<String, Object> map) {
        try {
            log.debug("CompanyMQConsumer received message: {}", map.getClass().getName());
            log.info("map{}", map.get("company").getClass().getName());
            Map<String, Object> companyMap = (Map<String, Object>) map.get("company");
            Company company = Company.builder()
                    .companyId((Integer) companyMap.get("companyId"))
                    .companyName((String) companyMap.get("companyName"))
                    .mdmPkCorp((String) companyMap.get("mdmPkCorp"))
                    .mdmCode((String) companyMap.get("mdmCode"))
                    .build();
//            Company company = JSON.parseObject(
//                     map.get("company").toString(),
//                    new TypeReference<Company>() {
//                    });
            //上面是旧方案
            String fathercorp = (String) (map.get("fathercorp"));
            companyService.completeCompanyDetailsAsync(company, fathercorp);
            log.info("CompanyMQConsumer processed message with company: {} and fathercorp: {}", company, fathercorp);
        } catch (Exception e) {
            log.error("Error processing message in CompanyMQConsumer: {}", e.getMessage(), e);
        }
    }
}
